//
//  ListView.m
//  BaseAPP
//
//  Created by Evisu on 16/12/19.
//  Copyright © 2016年 evisum. All rights reserved.
//

#import "ListView.h"
#import "ListViewCell.h"

@implementation ListView

- (instancetype)init {

    if (self = [super init]) {
        
        self.lists = [NSArray array];
        
        [self initViews];
        [self layoutViews];
    }
    
    return self;
}

- (void)initViews {
    
    self.ivBgView = [[UIImageView alloc] init];
    UIImage *bgImg = [UIImage imageNamed:@"MoreFunctionFrame_70x62_"];
    UIEdgeInsets insets = UIEdgeInsetsMake(26, 16, 20, 56);
    bgImg = [bgImg resizableImageWithCapInsets:insets resizingMode:UIImageResizingModeStretch];
    self.ivBgView.image = bgImg;
    
    self.tableView = [[UITableView alloc] init];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.bounces = NO;
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    [self addSubview:self.ivBgView];
    [self addSubview:self.tableView];
}


- (void)layoutViews {

    self.ivBgView.sd_layout
    .topEqualToView(self)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .bottomEqualToView(self);
    
    self.tableView.sd_layout
    .topSpaceToView(self, 10)
    .leftEqualToView(self)
    .rightEqualToView(self)
    .bottomEqualToView(self);
}

#pragma mark - tableView dataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return _lists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSArray *modelArray = _lists[indexPath.row];
    
    static NSString *reusedId = @"list";
    
    ListViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reusedId];
    
    if (!cell) {
        
        cell = [[ListViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reusedId];
    }
    
    cell.model = modelArray;
    cell.btnItem.tag = indexPath.row;
    
    cell.listBlock = ^(long tag){
    
        if ([self.delegate respondsToSelector:@selector(listViewDidClickedIndex:)]) {
            
            [self.delegate listViewDidClickedIndex:tag];
        }
    };
    
    return cell;
}

#pragma mark - tableView delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 44.0;
}


@end
